package io.linfeng.service.im.server.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.linfeng.common.utils.DateUtil;
import io.linfeng.common.utils.WebSocketUtil;
import io.linfeng.service.im.entity.MessageRecordEntity;
import io.linfeng.service.im.entity.SocketMsg;
import io.linfeng.service.im.server.MessageRecordService;
import io.linfeng.service.im.server.WebSocketService;
import io.linfeng.service.user.entity.UserEntity;
import io.linfeng.service.user.service.UserService;
import org.apache.catalina.util.RequestUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DateFormat;

/**
 * @author RJF
 * @date create at 2024-05-2024/5/7-19:17
 * copyright @2024 China Automotive Technology & Research Center Co.Ltd
 */
@Service("webSocketService")
public class WebSocketServiceImpl implements WebSocketService {

    @Resource
    private UserService userService;
    @Resource
    private MessageRecordService messageRecordService;
    @Resource
    private WebSocketUtil socketUtil;

    @Override
    public boolean privateMessage(SocketMsg socketMsg) {
        // 将用户发的消息持久化到数据库
        MessageRecordEntity entity = new MessageRecordEntity();
        entity.setSendOutUserId(socketMsg.getSendOutId()); //消息发送者
        entity.setReceiveUserId(socketMsg.getReceiveId()); //消息接受者
        entity.setMsg(socketMsg.getMsg()); //发送的消息
        entity.setType(socketMsg.getType()); //消息类型 群聊或者私聊
        entity.setGroupName(socketMsg.getGroupName()); //群聊名称
        entity.setSendTime(DateUtil.nowDateTime());
        boolean save = messageRecordService.save(entity);
        socketUtil.privateMessage(socketMsg);
        return save;
    }

    @Override
    public void publicMessage(String userName, String message, boolean flag) {
        socketUtil.publicMessage(userName,message,flag);
    }
}
